<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /* 
            滑动窗口-枚举右指针-循环外部更新答案
            时间复杂度：O(n)，并不是两个循环就是O(n)，这里的while循环条件sum - nums[left] >= target复杂度为O(1)
            空间复杂度：O(1)
        */
        var numSubarrayProductLessThanK = function(nums, k) {
            let start = 0
            let end = 0
            let ans = 0
            let len = nums.length
            let prod = 1
            // 枚举右指针
            while (end < len) {
                // 窗口扩展，右指针的值拉进来
                prod *= nums[end]
                while (prod >= k) {
                    // 如果不符合条件，则把start值排除出去
                    prod /= nums[start]
                    start++ 
                } 
                // 易错点：以right为右端点的子数组的数量 [start, right], [start + 1, right], [start + 2, right], [start + 3, right]……总共就是right - start + 1个
                ans += end - start + 1
                end++
            }
            return ans
        };
        console.log(numSubarrayProductLessThanK([10, 5, 2, 6], 100));
        // console.log(minSubArrayLen(4, [1, 4, 4]));
        // console.log(minSubArrayLen(11, [1, 1, 1, 1, 1, 1, 1, 1]));
        // console.log(minSubArrayLen(213, [12,28,83,4,25,26,25,2,25,25,25,12]));
        
    </script>
</body>
</html>